package com.star.page.service.impl;

import com.alibaba.fastjson.JSON;
import com.star.goods.feign.CategoryFeign;
import com.star.goods.feign.SkuFeign;
import com.star.goods.feign.SpuFeign;
import com.star.goods.pojo.Category;
import com.star.goods.pojo.Sku;
import com.star.goods.pojo.Spu;
import com.star.page.service.PageService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*
			  ________   ___   ___        ___   ________   ___   ___        ___
	 ________|\   __  \_|\  \_|\  \______|\  \_|\   __  \_|\  \_|\  \______|\  \______
	 \       \ \  \_\ /_\ \  \\ \  \     \ \  \\ \  \_\ /_\ \  \\ \  \     \ \  \     \
	  \       \ \   __  \\ \  \\ \  \     \ \  \\ \   __  \\ \  \\ \  \     \ \  \     \
	   \       \ \  \_\  \\ \  \\ \  \____ \ \  \\ \  \_\  \\ \  \\ \  \____ \ \  \     \
	    \       \ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\\ \_______\\ \__\     \
	     \       \|_______| \|__| \|_______| \|__| \|_______| \|__| \|_______| \|__|      \
	 	  \________________________________________________________________________________\

*/
@Service
public class PageServiceImpl implements PageService {

    @Value("${pagepath}")
    private String pagePath;
    @Autowired
    private SpuFeign spuFeign;
    @Autowired
    private SkuFeign skuFeign;
    @Autowired
    private CategoryFeign categoryFeign;
    @Autowired
    private TemplateEngine templateEngine;
    /**
     * 生成静态页面
     * @param spuId
     */
    @Override
    public void generateHtml(String spuId) {
        //1.获取context对象，context用于存储商品相关数据
        Context context = new Context();
        //获取静态页面需要的相关数据
        Map<String,Object> itemData=this.getItemData(spuId);
        context.setVariables(itemData);
        //2.获取商品详情页储存位置
        File dir = new File(pagePath);
        //3.判断存储文件夹是否存在，不存在则创建

        if (!dir.exists()) {
            //文件夹不存在，创建文件夹
            dir.mkdirs();
        }
        //4.定义输出流完成文件生成
        File file = new File(dir + "/" + spuId + ".html");
        Writer out = null;
        try {

            out = new PrintWriter(file);
            //生成静态页面
            /*
             * 1.模板名字
             * 2.context对象
             * 3.指定的输出流
             */
            templateEngine.process("item", context, out);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //5.关闭流
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 用于生成生成页面时所需数据的map集合
     * @param spuId
     * @return
     */
    private Map<String,Object> getItemData(String spuId) {
        Map<String, Object> resultMap = new HashMap<>();

        //获取spu数据
        Spu spuData = spuFeign.findSpuById(spuId).getData();
        resultMap.put("spu", spuData);
        //获取图片信息
        if (spuData != null) {
            if (StringUtils.isNotEmpty(spuData.getImages())) {
                resultMap.put("imageList", spuData.getImages().split(","));
            }
        }
        //获取商品分类信息
        //获取一二三级分类
        Category category1 = categoryFeign.findById(spuData.getCategory1Id()).getData();
        Category category2 = categoryFeign.findById(spuData.getCategory2Id()).getData();
        Category category3 = categoryFeign.findById(spuData.getCategory3Id()).getData();

        resultMap.put("category1", category1);
        resultMap.put("category2", category2);
        resultMap.put("category3", category3);
        //获取sku相关信息
        List<Sku> skuList = skuFeign.findListBySpuId(spuId);
        resultMap.put("skuList", skuList);

        //获取规格相关数据
        String specItems = spuData.getSpecItems();
        resultMap.put("specificationList", JSON.parseObject(specItems, Map.class));

        return resultMap;
    }
}
